package com.itheima.travel.service.impl;

import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.CategoryMapper;
import com.itheima.travel.pojo.Category;
import com.itheima.travel.pojo.CategoryExample;
import com.itheima.travel.req.CategoryVo;
import com.itheima.travel.service.CategoryService;
import com.itheima.travel.service.RedisCacheService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisCacheService redisCacheService;

    @Override
    public List<CategoryVo> findAllCategory() {
//        CategoryExample categoryExample = new CategoryExample();
//        List<Category> list = categoryMapper.selectByExample(categoryExample);
//        if(!EmptyUtil.isNullOrEmpty(list)){
//            List<CategoryVo> listVo = BeanConv.toBeanList(list, CategoryVo.class);
//            return listVo;
//        }
//        return null;
        return redisCacheService.listCache(()->{
            CategoryExample example = new CategoryExample();
            List<Category> categories = categoryMapper.selectByExample(example);
            if (!EmptyUtil.isNullOrEmpty(categories)){
                return BeanConv.toBeanList(categories, CategoryVo.class);
            }
            return null;
        }, RedisConstant.CATEGORYSERVICE_FINDALLCATEGORY);

    }
}
